/*
 * demo.cpp
 *
 *  Created on: 2015年5月7日
 *      Author: chuanjiang.zh@qq.com
 */

#include <stdio.h>
#include <stdlib.h>
#include <memory>
#include <iostream>
#include "CLog.h"

#include "DbConnectionPool.h"
#include "DbConnection.h"

/**
 * 解决以下问题：
 * # 连接字符串  --
 * # 检测断开，重连
 * # printf样式操作
 * # 简化接口
 * # 辅助转换
 *
 */


struct Num
{
    int count;
};

bool fromRow(Num& num, db::DbRow& row)
{
	//r.get(0, num.count);
	num.count = row.opt(0, 0);

	std::string str;
    row.get(0, str);

    return true;
}

int main(int argc, char** argv)
{
    std::string ip = "127.0.0.1";
    if (argc > 1)
    {
        ip = argv[1];
    }

	CLog::setLogger(CLog::COUT);
    CLog::flushOn(CLog::kDebug);

    puts("enter q to exit.\n");

    db::DbSource source;
    source.type = db::DB_MYSQL;
    source.ip = ip;
    source.port = 3306;
    source.user = "root";
    source.password = "Qwer1234";
    source.schema = "ivs";

    //source.type = db::DB_SQLITE;
    //source.schema = "demo.s3db";

    size_t maxSize = 4;
    db::DbConnectionPool dbPool(maxSize);
    dbPool.open(source);

    while (true)
    {
        std::cout << "$";

        std::string line;
        std::getline(std::cin, line);
        if ((line == "q") || (line == "quit"))
        {
            break;
        }

        db::DbConnection conn(&dbPool);
        if (!conn.validate())
        {
            continue;
        }

        if (line == "obj")
        {
            Num num;
            conn.queryObject("select count(*) from user", num);
            continue;
        }
		else if (line == "alarm")
		{
			Num num;
			conn.queryObject("select count(*) from alarm", num);
            CLog::info("count: %d\n", num.count);
			continue;
		}
		else if (line == "user")
		{
			Num num;
			conn.queryObject("select * from user", num);
			continue;
		}
        else if (line == "utf8")
        {
            std::string name;
            conn.queryValue("select name from user_info where username='admin'", name);
            std::cout << name;
            
            continue;
        }
        else if (line == "insert")
        {
            std::string sql = "insert into alarm  (`id`, `device`, `type`, `priority`, `dev_start`, `dev_stop`, `start_time`, `stop_time`, `image`, `description`, `boxes`, `zones`, `lines`, `params`, `handled`, `remark`, `person`, `unit`, `year`, `month`, `day`, `hour`, `wday`, `face_uuid`, `people_count`, `users`, `camera`, `people_info`)  values(NULL, '32322301271000', 'JT_001', 1, 1706068426673, 1706068432667, 1706068432682, 0, 0, '2024-01-24 11:53:46', '[]', '[]', '[]', '', 0, '', '', '',  2024,1,24,11,3,'dev', 1, '', 'C32322301271000', '46')";

            int64_t rid = 0;
            bool done = conn.insert(sql.c_str(), "alarm", rid);
            CLog::info("insert. done: %d, rid: %lld\n", done, rid);
            continue;
        }
		else if (line == "now")
		{
			line = "select now()";
            std::string value;
            conn.queryValue(line, value);
		}
        else
        {
            std::string sql;
            sql = line.c_str();
            if (sql.size() > 0)
            {
                bool done = conn.execute(sql);
                CLog::info("execute. done: %d\n", done);
            }
        }
    }

    return EXIT_SUCCESS;
}



